package com.bkgy.modular.unitOperationTime.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baosight.hdsdk.domain.data.HDDataConnection;
import com.baosight.hdsdk.domain.data.HDRecord;
import com.baosight.hdsdk.exception.HDSdkException;
import com.bkgy.modular.unitOperationTime.entity.TLovControlcenterC621;
import com.bkgy.modular.unitOperationTime.mapper.TLovControlcenterC621Mapper;
import com.bkgy.modular.unitOperationTime.service.UnitOperationTimeService;
import com.bkgy.util.SDKUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @ClassName UnitOperationTimeServiceImpl
 * @date 2023/3/23 16:11
 */
@Service
public class UnitOperationTimeServiceImpl extends ServiceImpl<TLovControlcenterC621Mapper, TLovControlcenterC621> implements UnitOperationTimeService {

    @Resource
    private TLovControlcenterC621Mapper tLovControlcenterC621Mapper;

    @Resource
    private SDKUtils sdkUtils;

    // @Scheduled(cron = "${c621Time}")
    public void findByC621Data() {
         log.error("UnitOperationTimeServiceImpl+findByC621Data");
        // 获取连接
        HDDataConnection connect = sdkUtils.getConnect();
         while (!connect.isConnected()) {
             connect = sdkUtils.getConnect();
         }
        // 根据变量名去查询对应的tagID
        Integer tagId = sdkUtils.findTagId("C621_VM_MDV_01_4VFB", connect.getTagManager());
        // 根据当前系统时间作为结束时间(end_time),当前系统时间向前推5分钟作为开始时间(start_time)
        // 当前系统时间戳
        long endTimeMillis = System.currentTimeMillis();
        Date endDate = new Date(endTimeMillis);
        LocalDateTime endLocalDateTime = getDateChangeLocalDateTime(endDate);

        // 系统时间-5分钟的时间
        Date startDate = this.subtractTime(endDate, 300000);
        LocalDateTime startLocalDateTime = getDateChangeLocalDateTime(startDate);
        List<HDRecord> hdRecordList = sdkUtils.findMsgByIdAndTime(tagId, connect.getDataProvider(), startLocalDateTime, endLocalDateTime);

        // 创建集合: 用于存放小于60的时间
        List<String> strTimeList = new ArrayList<>();

        // 遍历hdRecordList集合 获取每一条数据
        for (HDRecord hdRecord : hdRecordList) {
            String valueStr = hdRecord.getValueStr();
            // 判断valueStr是否小于60,(获取第一个小于60的数据的时间)
            if (Double.parseDouble(valueStr) < 60) {
                // 将小于60的数据存放到集合中
                strTimeList.add(hdRecord.getTimeStampStr());
            }
        }

        // 遍历strList集合 取第一个数据作为停机开始时间
        if (!strTimeList.isEmpty()) {
            System.out.println("strTimeList.get(0) = " + strTimeList.get(0));
        }
         try {
             connect.dispose();
         } catch (HDSdkException e) {
             log.error("UnitOperationTimeServiceImpl+findByC621Data+connect释放失败");
         }
    }

    /**
     * Date转LocalDateTime
     * @param date
     * @return
     */
    private LocalDateTime getDateChangeLocalDateTime(Date date) {
        Instant Instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = Instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    /**
     *  加减对应时间后的日期
     * @param date  需要加减时间的日期
     * @param amount    加减的时间(毫秒)
     * @return  加减对应时间后的日期
     */
    public Date subtractTime(Date date, int amount) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String strTime = sdf.format(date.getTime() - amount);
            Date time = sdf.parse(strTime);
            return time;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
